package platform.util;

public class FormulaCheck {
	/**
	 * 用于构造函数
	 */
	private String temp;
	/**
	 * 存储拆分后的id字符串数组
	 */
	String[] cellArray;
	/**
	 * 存储拆分后的name字符串数组
	 */
	String[] nameArray;
	/**
	 * 左,右括号记数目
	 */
	private int leftbracket = 0, rightbracket = 0;
	/**
	 * 表示字符串读取位置的计数
	 */
	private int index = 0;
	/**
	 * 检测公式是否合法
	 * @param str
	 * @return
	 */
	public void check(String str,String name) throws Exception{
		this.temp=str;
		processwhitespace();//去除公式中多余的空格
		this.cellArray = temp.split("\\|");
		this.temp=name;
		processwhitespace();//去除公式中多余的空格
		this.nameArray = temp.split("\\|");
		
		processexpression();	
		if (leftbracket != rightbracket) {
			throw new Exception("缺少括号！");
		}
	}
	
	/**
	 * 去除公式中多余的空格
	 */
	private void processwhitespace() {
		String emptystring = "";
		String[] tempstring = temp.split(" ");
		for (int i = 0; i < tempstring.length; i++) {
			emptystring += tempstring[i];
		}
		temp = emptystring + "#";
	}
	
	/**
	 * 读取表达式
	 * @throws Exception 
	 */
	private void processexpression() throws Exception
	{
		for (; index < cellArray.length-1; index++) {
			conjoint();//判断两个操作数是否相连
			
			if("(".equals(cellArray[index])){
				if (iserrorexpression("("))
					error();
				leftbracket++;
				continue;
			}
			
			if(")".equals(cellArray[index])){
				if (index == 0)
					startError();
				else {
					if (iserrorexpression(")"))
						error();
					rightbracket++;
				}
				continue;
			}
			
			if("+".equals(cellArray[index])||"-".equals(cellArray[index])||
					"*".equals(cellArray[index])||"/".equals(cellArray[index])){
				if (index == 0)
					startError();
				else {
					if (iserrorexpression(cellArray[index]))
						error();
					continue;
				}
			}
		}
	}
				
	/**
	 * 判断是否是错误的表达式
	 * @param errortempstring
	 * @return
	 */
	private boolean iserrorexpression(String str) {
		boolean iserror = false;
		if("(".equals(str)){
			if(index==0){
				if (")".equals(cellArray[index+1]))
					iserror = true;
			}else{
				if (")".equals(cellArray[index-1])||")".equals(cellArray[index+1])||
						(!"+".equals(cellArray[index-1]))&&
						!"-".equals(cellArray[index-1])&&
						!"*".equals(cellArray[index-1])&&
						!"/".equals(cellArray[index-1])&&
						!"(".equals(cellArray[index-1]))
					iserror = true;
			}
		}else if(")".equals(str)){
			if ("(".equals(cellArray[index-1])||"(".equals(cellArray[index+1])||
					(!"+".equals(cellArray[index+1]))&&
					!"-".equals(cellArray[index+1])&&
					!"*".equals(cellArray[index+1])&&
					!"/".equals(cellArray[index+1])&&
					!")".equals(cellArray[index+1])&&
					!"#".equals(cellArray[index+1]))
				iserror = true;
		}else if("+".equals(cellArray[index])||"-".equals(cellArray[index])||
				"*".equals(cellArray[index])||"/".equals(cellArray[index])){
			if("(".equals(cellArray[index-1])||
					"+".equals(cellArray[index-1])||
					"-".equals(cellArray[index-1])||
					"*".equals(cellArray[index-1])||
					"/".equals(cellArray[index-1])||
					"+".equals(cellArray[index+1])||
					"-".equals(cellArray[index+1])||
					"*".equals(cellArray[index+1])||
					"/".equals(cellArray[index+1])||
					"#".equals(cellArray[index+1])||
					")".equals(cellArray[index+1]))
				iserror = true;
		}
		return iserror;
	}
	
	//判断两个操作数是否相连
	private void conjoint() throws Exception{
		if((!"(".equals(cellArray[index])&&
				!")".equals(cellArray[index])&&
				!"+".equals(cellArray[index])&&
				!"-".equals(cellArray[index])&&
				!"*".equals(cellArray[index])&&
				!"/".equals(cellArray[index])&&
				!"#".equals(cellArray[index]))&&
				(!"(".equals(cellArray[index+1])&&
						!")".equals(cellArray[index+1])&&
						!"+".equals(cellArray[index+1])&&
						!"-".equals(cellArray[index+1])&&
						!"*".equals(cellArray[index+1])&&
						!"#".equals(cellArray[index+1])&&
						!"/".equals(cellArray[index+1]))){
			throw new Exception("第"+nameArray[index]+"~"+nameArray[index+1]+"之间缺少操作符！");
		}
	}
	
	//提示错误位置
	private void error()throws Exception{
		throw new Exception("公式不合法,错误位置可能在:\""+nameArray[index]+"\"前后!");
	}
	
	private void startError()throws Exception{
		throw new Exception("公式不合法,不能以:\""+nameArray[index]+"\"开始!");
	}
}
